<!DOCTYPE html>
<html lang="en">
    <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta content="Bmob,BmobCloud,bomb,BaaS,mBaaS,PaaS,Serverless,FaaS,Function as a Service,Backend as a Service,serverless computing,cloud function,后端云,bmob后端云,小程序云,小程序后端,云数据库,云存储,文件存储,云函数,云端代码,定时任务,游戏后端,游戏云,用户系统,无服务器函数,移动开发,app开发,小程序开发,云端一体化,互联网中间件" name="keywords"/>
            <meta content="国内首家专注于移动应用Serverless云服务的平台,Bmob后端云让移动开发更简单,全方位一体化的后端服务平台" name="description"/>
            
            
            
            <link rel="shortcut icon" href="../../img/favicon.ico">
        
        <script type="text/javascript" src="//game.bmob.cn/static/doc_union.js"></script>
            <!--
            <title>快速入门 - Bmob文档中心</title>
            -->
            <title>数据存储 &middot; C# – Bmob后端云 </title>        
            
            <link href="../../css/bootstrap-custom.min.css" rel="stylesheet">
            <link href="../../css/font-awesome-4.5.0.css" rel="stylesheet">
            <link href="../../css/base.css" rel="stylesheet">
            <link rel="stylesheet" href="../../css/highlight.css">
            <link href="../../css/agate.css" rel="stylesheet">
            <link href="../../css/custom.css" rel="stylesheet" id="custom">
    </head>

    <body >

        <div class="navbar navbar-fixed-top" role="navigation">
    <div class="main-nav">
        <!-- Collapsed navigation -->
        <div class="navbar-header">
            <a class="navbar-brand" href="../..">
                <!--Bmob文档中心-->
                <img src="../../img/logo.png" alt="">
            </a>
        </div>

        <!-- Expanded navigation -->
        <div class="navbar-collapse collapse">

          <ul class="nav navbar-nav">
            
              <li class="pull-left">
                
					<a href="../..">文档首页</a>
                
              </li>
            
              <li class="pull-left active">
                
					<!--<a href="">数据服务</a>-->
					<li class="dropdown active">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">数据服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../android/">Android</a>
                                </li>
                            
                            
								<li >
									<a href="../ios/">iOS</a>
                                </li>
                            
                            
								<li class="active">
									<a href="./">C#</a>
                                </li>
                            
                            
								<li >
									<a href="../php/">PHP</a>
                                </li>
                            
                            
								<li >
									<a href="../go/">GO</a>
                                </li>
                            
                            
								<li >
									<a href="../restful/">REST API</a>
                                </li>
                            
                            
								<li >
									<a href="../wechat_app_new/rm/">JavaScript</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../cocos2d_x/">Cocos2D-X</a>
                                </li>
							
                            
								<li >
									<a href="../wechat_app/">小程序</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../wechat_app_new/">快应用</a>
                                </li>
							
                            
                                <li >
                                    <a href="../wechat_app_new/">Nodejs</a>
                                </li>
							
                            
                                <li >
                                    <a href="../wechat_app_new/">Cocos Creator</a>
                                </li>
							
                            
								<li >
									<a href="../wechat_app_new/rm/">小程序(新)</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../kotlin/">Kotlin</a>
                                </li>
							
                            
                                <li >
                                    <a href="../python/">Python</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../cloud_function/android/">云函数</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">云函数 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../cloud_function/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/java/">Java</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/python/">Python</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/csharp/">C#</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/restful/">REST API</a>
                                </li>
							
                            
								<li >
									<a href="../../cloud_function/web/">Web</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../sms/android/">短信服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">短信服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../sms/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/restful/">REST API</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">游戏实时后端</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">游戏实时后端 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../game/unity/quick_start/">Unity</a>
                                </li>
                            
                            
								<li >
									<a href="../../game/cocos_creator/quick_start/">Cocos Creator</a>
                                </li>
                            
                            
								<li >
									<a href="../../game/wechat_games/quick_start/">微信小游戏</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../game/cloud_function/develop_doc/">云函数</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../game/classic_case/">经典案例</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">即时通讯</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">即时通讯 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../im/android/">Android</a>
                                </li>
                            
                            
								<li >
									<a href="../../im/ios/">iOS</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../push/android/">推送服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">推送服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../push/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../push/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../push/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../push/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../push/restful/">REST API</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../other/common_problem/">其他</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">其他 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../other/common_problem/">常见问题</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/error_code/">错误码</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/data_safety/">数据安全</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/bql/">BQL</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li>
                  <a href="https://www.bmob.cn/repository/index" target="_blank">知识库</a>
              </li>
              <li>
                  <a href="http://doc.bmob.cn/video/index.html" target="_blank">视频教程</a>
              </li>
            </ul>
            
        </div>
    </div>
</div>

        <div class="pagebody" id="main-wrapper">
            <div class="sidebar">
                <div class="bs-sidebar hidden-print affix well" role="complementary">

	
		
	
		
				
				
				
					<div class="code-title">C#</div>
				
				
				
				
				
				
				
				
				
				
				
				
				
		
	
		
				
				
				
				
				
				
				
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
				
				
		
	


	<ul class="nav bs-sidenav">
		
			
		
			
				
					
						
					
						
					
						
							
								
									<li class="active">
										<a class="itm-l1" href="./">快速入门</a>
										
										<ul class="nav">
											
											<li class="active "><a href="#bmob">注册Bmob帐号</a>
												
											</li>
												
											<li class=""><a href="#_1">网站后台创建应用</a>
												
											</li>
												
											<li class=""><a href="#_2">获取应用密钥</a>
												
											</li>
												
											<li class=""><a href="#sdk">下载安装SDK</a>
												
											</li>
												
											<li class=""><a href="#_3">新建模型类</a>
												
											</li>
												
											<li class=""><a href="#appkey">初始化AppKey</a>
												
											</li>
												
											<li class=""><a href="#_4">添加一行数据</a>
												
											</li>
												
											<li class=""><a href="#_5">更多例子</a>
												
											</li>
												
										</ul>
										
									</li>
								
									<li class="">
										<a class="itm-l1" href="develop_doc/">开发文档</a>
										
									</li>
								
									<li class="">
										<a class="itm-l1" href="example/">示例/功能</a>
										
									</li>
								
							
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
				
			
		
			
		
			
		
			
		
			
		
			
		
			
		
	</ul>
</div>
            </div>
            <div class="content" role="main">
                <div class="wrap">
                    

<h2 id="bmob">注册Bmob帐号<a class="headerlink" href="#bmob" title="Permanent link">&para;</a></h2>
<p>在网址栏输入 www.bmob.cn 或者在百度输入“Bmob后端云”进行搜索，打开Bmob官网后，点击右上角的“注册”，在跳转页面填入你的姓名、邮箱、设置密码，确认后到你的邮箱激活Bmob账户，你就可以用Bmob轻松开发应用了。</p>
<h2 id="_1">网站后台创建应用<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h2>
<p>登录账号进入bmob后台后，点击后台界面左上角“创建应用”，在弹出框输入你应用的名称，然后确认，你就拥有了一个等待开发的应用。</p>
<p><img alt="" src="./image/rumen_zhuce.png" /></p>
<h2 id="_2">获取应用密钥<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h2>
<p>选择你要开发的应用，点击该应用下方对应的“应用密钥”</p>
<p><img alt="" src="./image/rumen_miyue_1.png" /></p>
<p>在跳转页面，获取<code>Application ID</code>和<code>REST API key</code>，此IDs将会在初始化SDK中使用到。</p>
<p><img alt="" src="./image/rumen_miyue_2.png" /></p>
<h2 id="sdk">下载安装SDK<a class="headerlink" href="#sdk" title="Permanent link">&para;</a></h2>
<p>Bmob C# SDK下载地址：<a href="https://github.com/bmob/BmobSharp/releases">https://github.com/bmob/BmobSharp/releases</a></p>
<ul>
<li>Windows</li>
</ul>
<p>下载后解压，将Windows文件夹下的<code>Bmob-Windows.dll</code>文件引用到你的项目工程中，如下图所示。</p>
<p><img alt="" src="./image/dll.png" /></p>
<p>demo：<a href="https://github.com/bmob/bmob-demo-csharp/tree/master/examples/bmob-desktop-demo">bmob-desktop-demo</a></p>
<ul>
<li>Unity</li>
</ul>
<p>下载后解压，将Unity文件夹下的<code>Bmob-Unity.dll</code>文件放置到你的项目工程的 <strong>Assets/libs/</strong> 目录下。</p>
<p>demo：<a href="https://github.com/bmob/bmob-demo-csharp/tree/master/examples/bmob-unity-demo">bmob-unity-demo</a>
开发环境(调试)搭建：请查询<a href="https://github.com/bmob/BmobSharp#开发环境搭建">BmobSharp的README文档</a>。</p>
<h2 id="_3">新建模型类<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h2>
<p>要想对Bmob云端的数据进行操作，需要创建和数据表对应的模型类。在Bmob中，模型类需要继承自<code>BmobTable</code>，类的实现如下。</p>
<pre><code>//Game表对应的模型类
class GameObject : BmobTable
{

    private String fTable;
    //以下对应云端字段名称
    public BmobInt score { get; set; }
    public String playerName { get; set; }
    public BmobBoolean cheatMode { get; set; }

    //构造函数
    public GameObject() { }

    //构造函数
    public GameObject(String tableName)
    {
        this.fTable = tableName;
    }

    public override string table
    {
        get
        {
            if (fTable != null)
            {
                return fTable;
            }
            return base.table;
        }
    }

    //读字段信息
    public override void readFields(BmobInput input)
    {
        base.readFields(input);

        this.score = input.getInt(&quot;score&quot;);
        this.cheatMode = input.getBoolean(&quot;cheatMode&quot;);
        this.playerName = input.getString(&quot;playerName&quot;);
    }

    //写字段信息
    public override void write(BmobOutput output, bool all)
    {
        base.write(output, all);

        output.Put(&quot;score&quot;, this.score);
        output.Put(&quot;cheatMode&quot;, this.cheatMode);
        output.Put(&quot;playerName&quot;, this.playerName);
    }
}
</code></pre>

<h2 id="appkey">初始化AppKey<a class="headerlink" href="#appkey" title="Permanent link">&para;</a></h2>
<ul>
<li>Windows</li>
</ul>
<p>在正式对Bmob后端云进行操作之前，需要先初始化AppKey/RestKey信息，也就是初始化之前获取的<code>Application ID/RestKey</code>信息，实现代码如下。</p>
<pre><code>        //创建Bmob实例
        private BmobWindows bmob;

        public BmobBaseForm()
            : base()
        {
            bmob = new BmobWindows();

            //初始化，这个ApplicationId/RestKey需要更改为你自己的ApplicationId/RestKey（ http://www.bmob.cn 上注册登录之后，创建应用可获取到ApplicationId/RestKey）
            Bmob.initialize(&quot;4414150cb439afdf684d37dc184e0f9f&quot;, &quot;e1deb317442129c125b228ddf78e5f22&quot;);

            //注册调试工具
            BmobDebug.Register(msg =&gt; { Debug.WriteLine(msg); });
        }

        public BmobWindows Bmob
        {
            get { return bmob; }
        }
</code></pre>

<ul>
<li>Unity</li>
</ul>
<p>选中摄像机，把BmobUnity对象拖拽到摄像机上，然后再Properties选项卡中设置 <strong>ApplicationId</strong> 和 <strong>RestKey</strong> 。</p>
<p><img alt="" src="./image/unity.png" /></p>
<p>在脚本中启动方法中获取BmobUntiy：</p>
<pre><code>        private static BmobUnity Bmob;

        // Use this for initialization
        void Start ()
        {
                BmobDebug.Register (print);
                BmobDebug.level = BmobDebug.Level.TRACE;
                Bmob = gameObject.GetComponent&lt;BmobUnity&gt; ();
        }
</code></pre>

<h2 id="_4">添加一行数据<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h2>
<p>初始化AppKey之后，我们就可以对Bmob云数据库进行操作了。下面以添加一行数据为例进行说明，实现代码如下:</p>
<pre><code>//对应要操作的数据表
public const String TABLE_NAME = &quot;Game&quot;;
//接下来要操作的数据的数据
private GameObject gameObject = new GameObject(TABLE_NAME);

private void createData_Click(object sender, EventArgs e)
{
    //设置值    
    System.Random rnd = new System.Random();
    gameObject.score = rnd.Next(-50, 170);
    gameObject.playerName = &quot;123&quot;;
    gameObject.cheatMode = false;

    //保存数据
    var future = Bmob.CreateTaskAsync(gameObject);
    //异步显示返回的数据
    FinishedCallback(future.Result, resultText);
}
</code></pre>

<h2 id="_5">更多例子<a class="headerlink" href="#_5" title="Permanent link">&para;</a></h2>
<p>大家可详细查看<a href="https://github.com/bmob/bmob-demo-csharp/tree/master/examples/bmob-desktop-demo/">示例源码</a>和 <a href="https://github.com/bmob/BmobSharp">SDK项目源码</a>，了解更多的Bmob C# SDK for Windows的操作。</p>
                </div>
            </div>
        </div>

        
            <script>var base_url = '../..';</script>
            <script src="../../js/jquery-1.10.2.min.js"></script>
            <script src="../../js/bootstrap-3.0.3.min.js"></script>
            <script src="../../js/highlight.pack.js"></script>
            <script src="../../js/main.js"></script>
            <script src="../../js/base.js"></script>

        <div id="go-top"><i class="fa fa-chevron-up"></i></div>
        <a href="https://docs.bmob.cn/data/Android/a_faststart/doc/index.html" id="back" target="_blank">返回 <br> 旧版</a>
    </body>

</html>